﻿#include "midregiongroupform.h"
#include "ui_midregiongroupform.h"
#include <QSettings>
#include <QDir>
#include<QDebug>
int number=0;
int preRegion=0;
//是否需要将regionInfo保存到配置文件，每次重新读取？？？

QVector<double> realstart;
QVector<double> realend;
MidRegionGroupForm::MidRegionGroupForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MidRegionGroupForm)
{
    ui->setupUi(this);
    this->setObjectName("WidgetObjectRegionScanForm-midregionscan");
    this->setWindowTitle(tr("MidRegionGroupForm"));

    QDir settingDir(QDir::currentPath());
    settingDir.cdUp();
    settingDir.cd("config/FY4TaskOptimization");
    QSettings settings(settingDir.absoluteFilePath("setting.ini"),QSettings::IniFormat);
    settings.beginGroup(TaskManager::getInstance()->curIns().append("-MidRegion"));

    ui->doubleSpinBox_Cycle->setValue(LoopTime);
    ui->doubleSpinBox_ScanSpeed->setValue(TaskManager::getInstance()->currentInstrument()->mdMirrorScanSpeed);

    double dReg1X = 0.87;
    double dReg1Y = 3.6;
    double dReg2X = 0.42;
    double dReg2Y = -3.74;
    double regionx=1024;
    double regiony=1024;


    // 第一个区域
    if(settings.contains("midregion1x"))
        dReg1X = settings.value("midregion1x").toDouble();
    else
    {
        settings.setValue("midregion1x",QString::number(dReg1X,'f',6));
    }

    if(settings.contains("midregion1y"))
        dReg1Y = settings.value("midregion1y").toDouble();
    else
    {
        settings.setValue("midregion1y",QString::number(dReg1Y,'f',6));
    }

    // 第二个区域
    if(settings.contains("midregion2x"))
        dReg2X = settings.value("midregion2x").toDouble();
    else
    {
        settings.setValue("midregion2x",QString::number(dReg2X,'f',6));
    }

    if(settings.contains("midregion2y"))
        dReg2Y = settings.value("midregion2y").toDouble();
    else
    {
        settings.setValue("midregion2y",QString::number(dReg2Y,'f',6));
    }
    if(settings.contains("midregionweight"))
        regionx=settings.value("midregionweight").toInt();
    else
    {
        settings.setValue("midregionweight",regionx);
    }
    if(settings.contains("midregionheight"))
        regiony=settings.value("midregionheight").toInt();
    else
    {
        settings.setValue("midregionheight",regiony);
    }

    ui->tableWidget_Region->setRowCount(2);
    ui->tableWidget_Region->setColumnCount(4);
    //待完善：考虑将中尺度坐标改成“x，y”格式！！！！！
    //if(!settings.contains(QString("InfraredBackground%1").arg(i+1)))
    //{
     //   settings.setValue(QString("InfraredBackground%1").arg(i+1),QString("%1,%2").arg(dEWPos).arg(dNSPos));

    //QString sContext = settings.value(QString("InfraredBackground%1").arg(i+1)).toString();
    //ui->PointWidget->setItem(i,Table_EWEnd,new QTableWidgetItem(sContext.section(",",0,0)));
   // ui->PointWidget->setItem(i,Table_NSEnd,new QTableWidgetItem(sContext.section(",",1,1)));
    ui->tableWidget_Region->setItem(0,0,new QTableWidgetItem(QString::number(dReg1X,'f',6)));
    ui->tableWidget_Region->setItem(0,1,new QTableWidgetItem(QString::number(dReg1Y,'f',6)));
    ui->tableWidget_Region->setItem(1,0,new QTableWidgetItem(QString::number(dReg2X,'f',6)));
    ui->tableWidget_Region->setItem(1,1,new QTableWidgetItem(QString::number(dReg2Y,'f',6)));
    ui->tableWidget_Region->setItem(0,2,new QTableWidgetItem(QString::number(regionx)));
    ui->tableWidget_Region->setItem(0,3,new QTableWidgetItem(QString::number(regiony)));
    ui->tableWidget_Region->setItem(1,2,new QTableWidgetItem(QString::number(regionx)));
    ui->tableWidget_Region->setItem(1,3,new QTableWidgetItem(QString::number(regiony)));

    settings.endGroup();

}

MidRegionGroupForm::~MidRegionGroupForm()
{
    delete ui;
}
void MidRegionGroupForm::generateTask(TaskItem *pTask)
{
    int row=0;
    if(!msCurTaskName.isEmpty() && NULL == pTask)// 更新任务
    {
        pTask = TaskManager::getInstance()->getTask(msCurTaskName);
        QVector<CommandItem *> pcommands=pTask->allCommands();
        int temp=0;
        CommandItem::FastTargetPos fastpos;
        for(int i=0; i<ui->tableWidget_Command->rowCount(); ++i)
        {

            if(temp>=pcommands.size())
                break;
            CommandItem* pCommandItem =pTask->getCommand(pcommands.at(temp++)->objectName());

            fastpos.miTargetPosWorkMode="wait";

            fastpos.mdTargetPosDuration=MidReginstableTime;
            pCommandItem->setFastPosMode(fastpos);
            pCommandItem->setValue(ScriptItem::Pos_realEWstart,realstart[row]);
            pCommandItem->setValue(ScriptItem::Pos_realEWend,realend[row]);
            pCommandItem->setValue(ScriptItem::Pos_EWStart,ui->tableWidget_Command->item(i,1)->text().toDouble());
            pCommandItem->setValue(ScriptItem::Pos_EWEnd,ui->tableWidget_Command->item(i,2)->text().toDouble());
            pCommandItem->setValue(ScriptItem::Pos_NSStart,ui->tableWidget_Command->item(i,3)->text().toDouble());
            pCommandItem->setValue(ScriptItem::Pos_NSEnd,ui->tableWidget_Command->item(i,4)->text().toDouble());
            pCommandItem->setValue(ScriptItem::Time_Start,ui->tableWidget_Command->item(i,5)->text().toDouble());
            pCommandItem->setValue(ScriptItem::Time_End,ui->tableWidget_Command->item(i,7)->text().toDouble());
            ++row;
        }
    }
    else
    {

        CommandItem::FastTargetPos fastpos;
        for(int i=0; i<ui->tableWidget_Command->rowCount(); ++i)
        {
            CommandItem *pCommandItem=NULL;
            pCommandItem = pTask->addCommand(ui->tableWidget_Command->item(i,0)->text());
            pCommandItem->setWorkMode(CommandItem::Mode_SlideScan);
            fastpos.mdTargetPosDuration=instructionIntervalTime;
            pCommandItem->setFastPosMode(fastpos);
            pCommandItem->setValue(ScriptItem::Pos_realEWstart,realstart[row]);
            pCommandItem->setValue(ScriptItem::Pos_realEWend,realend[row]);
            pCommandItem->setValue(ScriptItem::Pos_EWStart,ui->tableWidget_Command->item(i,1)->text().toDouble());
            pCommandItem->setValue(ScriptItem::Pos_EWEnd,ui->tableWidget_Command->item(i,2)->text().toDouble());
            pCommandItem->setValue(ScriptItem::Pos_NSStart,ui->tableWidget_Command->item(i,3)->text().toDouble());
            pCommandItem->setValue(ScriptItem::Pos_NSEnd,ui->tableWidget_Command->item(i,4)->text().toDouble());
            pCommandItem->setValue(ScriptItem::Time_Start,ui->tableWidget_Command->item(i,5)->text().toDouble());
            pCommandItem->setValue(ScriptItem::Time_End,ui->tableWidget_Command->item(i,7)->text().toDouble());
            ++row;
        }
    }
}
void MidRegionGroupForm::setCurrentTask(const QString &sName)
{
    msCurTaskName = sName;

    TaskEditor::setCurrentTask(sName);
}
void MidRegionGroupForm::setCurrentMode(int nMode)
{
    // 不属于添加模式，部分按钮应该隐藏
    if(nMode != 0)
    {
        ui->spinBoxt_times->setValue(number);
        ui->spinBoxt_times->setEnabled(false);
        ui->pushButton_AddRegion->setEnabled(false);
        ui->pushButton_RemoveRegion->setEnabled(false);
        if(!TaskManager::getInstance()->mvctregion.isEmpty())
        {
            for(int i=preRegion-1;i>=0;--i)
                ui->tableWidget_Region->removeRow(i);

            //0826新增
            //查找当前编辑的任务是tree中的第几个中尺度任务，第一个就找allregion中的第一个mvctregion，第2个就找第二个

            int midnum=TaskManager::getInstance()->getcurMidNum();
            if(midnum)
            {
            QVector<struct regionInfo> cureditregion=TaskManager::getInstance()->allregionInfos.at(midnum-1);
            ui->spinBoxt_times->setValue(cureditregion.at(0).cycleNum);

            ui->tableWidget_Region->setRowCount(cureditregion.size());
            ui->tableWidget_Region->setColumnCount(4);

            for(int i=0;i<cureditregion.size();++i)
            {
                ui->tableWidget_Region->setItem(i,0,new QTableWidgetItem(QString("%1").arg(cureditregion.at(i).midx)));
                ui->tableWidget_Region->setItem(i,1,new QTableWidgetItem(QString("%1").arg(cureditregion.at(i).midy)));
                ui->tableWidget_Region->setItem(i,2,new QTableWidgetItem(QString("%1").arg(cureditregion.at(i).widget)));
                ui->tableWidget_Region->setItem(i,3,new QTableWidgetItem(QString("%1").arg(cureditregion.at(i).height)));
            }
        }
        ui->tableWidget_Region->setEnabled(false);
        }
    }
    TaskEditor::setCurrentMode(nMode);
}
void MidRegionGroupForm::updateTask(TaskItem *pTask)
{
    if(NULL == pTask)
        return;
    msCurTaskName = pTask->objectName();

    QVector<CommandItem *> vctCmds = pTask->allCommands();
    ui->tableWidget_Command->setRowCount(vctCmds.count());
    for(int i=0; i<vctCmds.count(); ++i){
        QTableWidgetItem* pTableItem = new QTableWidgetItem(vctCmds.at(i)->getName());
        // 设置对象名称，为了在计算的时候不重新添加，同时可在更新的时候方便查找
        pTableItem->setData(Qt::WhatsThisRole,vctCmds.at(i)->objectName());

        ui->tableWidget_Command->setItem(i,Table_Name,pTableItem);
        ui->tableWidget_Command->setItem(i,Table_EWStart,new QTableWidgetItem(QString::number(vctCmds.at(i)->getValue(ScriptItem::Pos_EWStart),'f',6)));
        ui->tableWidget_Command->setItem(i,Table_EWEnd,new QTableWidgetItem(QString::number(vctCmds.at(i)->getValue(ScriptItem::Pos_EWEnd),'f',6)));
        ui->tableWidget_Command->setItem(i,Table_NSStart,new QTableWidgetItem(QString::number(vctCmds.at(i)->getValue(ScriptItem::Pos_NSStart),'f',6)));
        ui->tableWidget_Command->setItem(i,Table_NSEnd,new QTableWidgetItem(QString::number(vctCmds.at(i)->getValue(ScriptItem::Pos_NSEnd),'f',6)));

        double dTimeStart = vctCmds.at(i)->getValue(ScriptItem::Time_Start);
        double dTimeEnd = vctCmds.at(i)->getValue(ScriptItem::Time_End);

        ui->tableWidget_Command->setItem(i,Table_STime,new QTableWidgetItem(QString::number(dTimeStart,'f',6)));
        ui->tableWidget_Command->setItem(i,Table_MidTime,new QTableWidgetItem(QString::number((dTimeStart+dTimeEnd)/2,'f',6)));
        ui->tableWidget_Command->setItem(i,Table_ETime,new QTableWidgetItem(QString::number(dTimeEnd,'f',6)));
        ui->tableWidget_Command->setItem(i,Table_TotTime,new QTableWidgetItem(QString::number(dTimeEnd-dTimeStart,'f',6)));
        if(i==0)
        {
            ui->tableWidget_Command->setCurrentItem(pTableItem);
        }
    }
}
void MidRegionGroupForm::on_pushButton_AddRegion_clicked()
{
    int nRow = ui->tableWidget_Region->currentRow()+1;
    if(nRow < 0)
        nRow = 0;

    ui->tableWidget_Region->insertRow(nRow);
    ui->tableWidget_Region->setItem(nRow,0,new QTableWidgetItem(QString("0.0")));
    ui->tableWidget_Region->setItem(nRow,1,new QTableWidgetItem(QString("0.0")));
    ui->tableWidget_Region->setItem(nRow,2,new QTableWidgetItem(QString("1024")));
    ui->tableWidget_Region->setItem(nRow,3,new QTableWidgetItem(QString("1024")));
}

void MidRegionGroupForm::on_pushButton_RemoveRegion_clicked()
{
    int nCurRow = ui->tableWidget_Region->currentRow();
    if(nCurRow < 0)
        nCurRow = 0;

    ui->tableWidget_Region->removeRow(nCurRow);
}
void MidRegionGroupForm::on_pushButton_AddCycle_clicked()
{
    for(int i=ui->tableWidget_Command->currentRow(); i<ui->tableWidget_Command->rowCount(); ++i)
    {
        double dTotTime =  ui->tableWidget_Command->item(i,8)->text().toDouble();
        //  中间时间对齐
        if(ui->comboBox_TimeAlign->currentIndex() == 0)
        {
            double dAlighTime =ui->tableWidget_Command->item(i,6)->text().toDouble() + ui->doubleSpinBox_Cycle->value();

            ui->tableWidget_Command->item(i,5)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
            ui->tableWidget_Command->item(i,6)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget_Command->item(i,7)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
            ui->tableWidget_Command->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
        // 开始时间对齐
        else if(ui->comboBox_TimeAlign->currentIndex() == 1)
        {
            double dAlighTime =ui->tableWidget_Command->item(i,5)->text().toDouble() + ui->doubleSpinBox_Cycle->value();

            ui->tableWidget_Command->item(i,5)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget_Command->item(i,6)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
            ui->tableWidget_Command->item(i,7)->setText(QString::number(dAlighTime + dTotTime,'f',6));
            ui->tableWidget_Command->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
        // 结束时间对齐
        else
        {
            double dAlighTime =ui->tableWidget_Command->item(i,7)->text().toDouble() + ui->doubleSpinBox_Cycle->value();

            ui->tableWidget_Command->item(i,5)->setText(QString::number(dAlighTime - dTotTime,'f',6));
            ui->tableWidget_Command->item(i,6)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
            ui->tableWidget_Command->item(i,7)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget_Command->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
    }
}

void MidRegionGroupForm::on_pushButton_DeCycle_clicked()
{
    for(int i=ui->tableWidget_Command->currentRow(); i>=0; --i)
    {
        double dTotTime =  ui->tableWidget_Command->item(i,8)->text().toDouble();
        //  中间时间对齐
        if(ui->comboBox_TimeAlign->currentIndex() == 0)
        {
            double dAlighTime =ui->tableWidget_Command->item(i,Table_MidTime)->text().toDouble() - ui->doubleSpinBox_Cycle->value();

            ui->tableWidget_Command->item(i,5)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
            ui->tableWidget_Command->item(i,6)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget_Command->item(i,7)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
            ui->tableWidget_Command->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
        // 开始时间对齐
        else if(ui->comboBox_TimeAlign->currentIndex() == 1)
        {
            double dAlighTime =ui->tableWidget_Command->item(i,5)->text().toDouble() - ui->doubleSpinBox_Cycle->value();

            ui->tableWidget_Command->item(i,5)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget_Command->item(i,6)->setText(QString::number(dAlighTime + dTotTime*0.5,'f',6));
            ui->tableWidget_Command->item(i,7)->setText(QString::number(dAlighTime + dTotTime,'f',6));
            ui->tableWidget_Command->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
        // 结束时间对齐
        else
        {
            double dAlighTime =ui->tableWidget_Command->item(i,7)->text().toDouble() - ui->doubleSpinBox_Cycle->value();

            ui->tableWidget_Command->item(i,5)->setText(QString::number(dAlighTime - dTotTime,'f',6));
            ui->tableWidget_Command->item(i,6)->setText(QString::number(dAlighTime - dTotTime*0.5,'f',6));
            ui->tableWidget_Command->item(i,7)->setText(QString::number(dAlighTime,'f',6));
            ui->tableWidget_Command->item(i,8)->setText(QString::number(dTotTime,'f',6));
        }
    }
}

void MidRegionGroupForm::on_pushButton_CalPos_clicked()
{
    realstart.clear();
    realend.clear();

    TaskManager::getInstance()->mvctregion.clear();

    for(int i=0;i<ui->tableWidget_Region->rowCount();++i)
    {
        struct regionInfo region;
        region.midx=ui->tableWidget_Region->item(i,0)->text().toDouble();
        region.midy=ui->tableWidget_Region->item(i,1)->text().toDouble();
        region.widget=ui->tableWidget_Region->item(i,2)->text().toDouble();
        region.height=ui->tableWidget_Region->item(i,3)->text().toDouble();
        region.cycleNum=ui->spinBoxt_times->text().toInt();

        TaskManager::getInstance()->mvctregion.append(region);
    }

    if(!TaskManager::getInstance()->mvctregion.isEmpty())
    {
        TaskManager::getInstance()->allregionInfos.append(TaskManager::getInstance()->mvctregion);
    }


    preRegion=TaskManager::getInstance()->mvctregion.size();
    number=ui->spinBoxt_times->value();

    double dMirrHeight = TaskManager::getInstance()->currentInstrument()->mdMirrorHeight;

    double dregionwidth=0.0;
    double dregionheight=0.0;
    double midtposx=0.0;
    double midtposy=0.0;

    //清空行
    while(ui->tableWidget_Command->rowCount()> 0)
    {
        ui->tableWidget_Command->removeRow(0);
    }

    //周期内对齐时间
    double dAlighTime = ui->doubleSpinBox_align->value();

    //任务中开始位置和结束位置
    double ewstart=0.0;
    double ewend=0.0;
    double nsstart=0.0;
    double nsend=0.0;

    //任务中开始结束中间时间
    double starttime=0.0;
    double endtime=0.0;
    double midtime=0.0;
    //针对结束时间对齐
    double movetime=0.0;

    //循环变量
    int align=0;

    // 循环添加每个区域
    for(int ii = 0; ii<ui->spinBoxt_times->value(); ++ii)
    {

        for(int i=0; i<ui->tableWidget_Region->rowCount(); ++i)
        {
            //第一个区域和第二个区域的中心点坐标和宽度高度；
            midtposx=(ui->tableWidget_Region->item(i,0)->text()).toDouble();
            midtposy=(ui->tableWidget_Region->item(i,1)->text()).toDouble();
            //用区域宽和高算出对应的机械角宽和高；
            //获取到值1024km，除0.5km看像元对应多少像元，乘0.000007转换成弧度，再转换为角度(这个角度就是1024km对应的角度，不分机械和视场)。
            dregionwidth=((ui->tableWidget_Region->item(i,2)->text()).toDouble()-1)/0.5*0.000007*180/M_PI;
            dregionheight=((ui->tableWidget_Region->item(i,3)->text()).toDouble()-1)/0.5*0.000007*180/M_PI;

            int ScanRowNumber=ceil(dregionheight/dMirrHeight);

            if(ui->comboBox_RegionMode->currentIndex() > 0)// 往返扫描模式
            {
                ScanRowNumber =fmod(ScanRowNumber,2)== 0? ScanRowNumber:ScanRowNumber+1;
            }

            //每个区域分几行
            for(int j=0; j<ScanRowNumber; ++j)
            {
                ewoldstart=ewstart;
                ewoldend=ewend;
                nsoldstart=nsstart;
                nsoldend=nsend;

                ewstart=midtposx-dregionwidth/2;
                ewend=midtposx+dregionwidth/2;

                // 中间对齐
                if(ui->comboBox_PosAlign->currentIndex() == 0)
                {
                    //第n行开始y=中心点y+镜子高度*（扫描行号/2-第n行）+镜子高度/2
                    nsstart=midtposy+dMirrHeight*((float)ScanRowNumber/2-(j+1))+dMirrHeight/2;
                    nsend=nsstart;

                }
                // 大数对齐，北侧，正向
                else if(ui->comboBox_PosAlign->currentIndex() == 1)
                {
                    nsstart=midtposy+dregionheight/2-dMirrHeight/2-dMirrHeight*(j);
                    nsend=nsstart;
                }
                // 小数对齐
                else
                {
                    nsstart=midtposy-dregionheight/2+dMirrHeight/2+(ScanRowNumber-(j+1))*dMirrHeight;
                    nsend=nsstart;
                }
                realstart.append(ewstart);
                realend.append(ewend);
                TaskManager::getInstance()->currentInstrument()->getActuallocation(ewstart,ewend,rlx,ui->doubleSpinBox_ScanSpeed->text().toDouble());

                ewstart=rlx[0];
                ewend=rlx[1];
                int nCommRowCount = ui->tableWidget_Command->rowCount();

                ui->tableWidget_Command->insertRow(nCommRowCount);

                QTableWidgetItem *pNameItem=new QTableWidgetItem(QString("%1 %2-%3/%4").arg(msCurTaskName).arg(i+1).arg(j+1).arg(ScanRowNumber));
                if(mnWorkMode==0)
                    pNameItem->setData(Qt::WhatsThisRole, QString("none"));

                ui->tableWidget_Command->setItem(nCommRowCount,0,pNameItem);
                ui->tableWidget_Command->setItem(nCommRowCount,1,new QTableWidgetItem(QString::number(rlx[0],'f',6)));
                ui->tableWidget_Command->setItem(nCommRowCount,2,new QTableWidgetItem(QString::number(rlx[1],'f',6)));
                ui->tableWidget_Command->setItem(nCommRowCount,3,new QTableWidgetItem(QString::number(nsstart,'f',6)));
                ui->tableWidget_Command->setItem(nCommRowCount,4,new QTableWidgetItem(QString::number(nsend,'f',6)));

                //扫描一行的时间
                double dTotTime=TaskManager::getInstance()->currentInstrument()->getScanTime(rlx[0],rlx[1],ui->doubleSpinBox_ScanSpeed->text().toDouble());
                //  中间时间对齐
                if(ui->comboBox_TimeAlign->currentIndex() == 0)
                {
                    //一个区域第一行
                    if(j==0)
                    {
                        //获取中间时间
                        midtime=dAlighTime+(align++)*ui->doubleSpinBox_Cycle->value();
                        starttime=midtime-dTotTime/2;
                        endtime=midtime+dTotTime/2;
                    }
                    //一个区域其他行
                    else
                    {
                        double preend=endtime;
                        starttime=preend+instructionIntervalTime+TaskManager::getInstance()->currentInstrument()->getQuickTime(ewoldend,nsoldend,ewstart,nsstart,TaskManager::getInstance()->currentInstrument()->mdFastPointSpeed)+MidReginstableTime;
                        endtime=starttime+dTotTime;
                        midtime=(starttime+endtime)/2;
                    }

                    ui->tableWidget_Command->setItem(nCommRowCount,5,new QTableWidgetItem(QString::number(starttime,'f',6)));
                    ui->tableWidget_Command->setItem(nCommRowCount,6,new QTableWidgetItem(QString::number(midtime,'f',6)));
                    ui->tableWidget_Command->setItem(nCommRowCount,7,new QTableWidgetItem(QString::number(endtime,'f',6)));
                    ui->tableWidget_Command->setItem(nCommRowCount,8,new QTableWidgetItem(QString::number(dTotTime,'f',6)));
                }
                // 开始时间对齐
                else if(ui->comboBox_TimeAlign->currentIndex() == 1)
                {
                    if(j==0)
                    {
                        //获取开始时间
                        starttime=dAlighTime+(align++)*ui->doubleSpinBox_Cycle->value();
                        endtime=starttime+dTotTime;
                        midtime=(endtime+starttime)/2;
                    }
                    else
                    {
                        double preend=endtime;
                        starttime=preend+instructionIntervalTime+TaskManager::getInstance()->currentInstrument()->getQuickTime(ewoldend,nsoldend,ewstart,nsstart,TaskManager::getInstance()->currentInstrument()->mdFastPointSpeed)+MidReginstableTime;
                        endtime=starttime+dTotTime;
                        midtime=(starttime+endtime)/2;
                    }
                    ui->tableWidget_Command->setItem(nCommRowCount,5,new QTableWidgetItem(QString::number(starttime,'f',6)));
                    ui->tableWidget_Command->setItem(nCommRowCount,6,new QTableWidgetItem(QString::number(midtime,'f',6)));
                    ui->tableWidget_Command->setItem(nCommRowCount,7,new QTableWidgetItem(QString::number(endtime,'f',6)));
                    ui->tableWidget_Command->setItem(nCommRowCount,8,new QTableWidgetItem(QString::number(dTotTime,'f',6)));
                }
                // 结束时间对齐//！！！！是不是应该是第二个区域结束时间相同那就要求出扫描两行一共的时间
                else
                {
                    if(j==0)
                    {
                        //获取结束时间
                        endtime=dAlighTime+(align++)*ui->doubleSpinBox_Cycle->value();
                        starttime=endtime-dTotTime;
                        midtime=(endtime+starttime)/2;
                    }
                    else
                    {
                        double preend=endtime;
                        starttime=preend+instructionIntervalTime+TaskManager::getInstance()->currentInstrument()->getQuickTime(ewoldend,nsoldend,ewstart,nsstart,TaskManager::getInstance()->currentInstrument()->mdFastPointSpeed)+MidReginstableTime;
                        endtime=starttime+dTotTime;
                        midtime=(starttime+endtime)/2;
                        //前移的时间
                        movetime=endtime-(dAlighTime+(align-1)*ui->doubleSpinBox_Cycle->value());
                    }

                    ui->tableWidget_Command->setItem(nCommRowCount,5,new QTableWidgetItem(QString::number(starttime,'f',6)));
                    ui->tableWidget_Command->setItem(nCommRowCount,6,new QTableWidgetItem(QString::number(midtime,'f',6)));
                    ui->tableWidget_Command->setItem(nCommRowCount,7,new QTableWidgetItem(QString::number(endtime,'f',6)));
                    ui->tableWidget_Command->setItem(nCommRowCount,8,new QTableWidgetItem(QString::number(dTotTime,'f',6)));
                }
            }
        }
    }
    //针对结束时间对齐情况！！！！！！！！
    if(ui->comboBox_TimeAlign->currentIndex() == 2)
    {
        //将所有行的开始时间和结束时间都减去move时间
        for(int i=0;i<ui->tableWidget_Command->rowCount();++i)
        {
            //取到当前行的开始中间和结束时间
            double newstart=ui->tableWidget_Command->item(i,Table_STime)->text().toDouble();
            double newmid=ui->tableWidget_Command->item(i,Table_MidTime)->text().toDouble();
            double newend=ui->tableWidget_Command->item(i,Table_ETime)->text().toDouble();
            ui->tableWidget_Command->item(i,Table_STime)->setText(QString::number(newstart-movetime,'f',6));
            ui->tableWidget_Command->item(i,Table_MidTime)->setText(QString::number(newmid-movetime,'f',6));
            ui->tableWidget_Command->item(i,Table_ETime)->setText(QString::number(newend-movetime,'f',6));
        }
    }
    on_comboBox_TimeAlign_currentIndexChanged(ui->comboBox_TimeAlign->currentIndex());
}

void MidRegionGroupForm::on_comboBox_TimeAlign_currentIndexChanged(int index)
{
    int AlignCol = -1;
    // 中间时间对齐
    if(index == 0)
    {
        AlignCol = 6;
    }
    // 开始时间对齐
    else if(index == 1)
    {
        AlignCol = 5;
    }
    // 结束时间对齐
    else
    {
        AlignCol = 7;
    }

    int iCurRow = ui->tableWidget_Command->currentRow();
    if(iCurRow < 0)
        iCurRow = 0;

    ui->tableWidget_Command->setCurrentCell(iCurRow,AlignCol);

    for(int i=0; i<ui->tableWidget_Command->rowCount(); ++i)
    {
        for(int j=0; j<ui->tableWidget_Command->columnCount(); ++j)
        {
            QTableWidgetItem* pItem = ui->tableWidget_Command->item(i,j);
            if(pItem == NULL)
                continue;

            if(i == iCurRow && j == AlignCol)
            {
                pItem->setIcon(QIcon(QPixmap(":/image/res/redflag.png")));
            }
            else
            {
                pItem->setIcon(QIcon());
            }
        }
    }
}

void MidRegionGroupForm::on_tableWidget_Command_currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn)
{
    int AlignCol = -1;
    // 中间时间对齐
    if(ui->comboBox_TimeAlign->currentIndex() == 0)
    {
        AlignCol = 6;
    }
    // 开始时间对齐
    else if(ui->comboBox_TimeAlign->currentIndex() == 1)
    {
        AlignCol = 5;
    }
    // 结束时间对齐
    else
    {
        AlignCol = 7;
    }

    if(ui->tableWidget_Command->item(previousRow,AlignCol) != NULL)
        ui->tableWidget_Command->item(previousRow,AlignCol)->setIcon(QIcon());

    if(NULL != ui->tableWidget_Command->item(currentRow,AlignCol))
        ui->tableWidget_Command->item(currentRow,AlignCol)->setIcon(QIcon(QPixmap(":/image/res/redflag.png")));
}

    //0528更新，加载任务时，中尺度区域循环次数为0.
    //如果次数为0，重新计算循环次数。
//    if(ui->spinBoxt_times->text().toInt()==0)
//    {
//        //全部行号/(每个区域的行号)=循环几次
//        int totalrow=ui->tableWidget_Command->rowCount();
//        QString base=ui->tableWidget_Command->item(0,0)->text();
//    }
